﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;

public partial class FolderPage : PageBaseClass
{
    MyDMSEntity _myDmsEntity = new MyDMSEntity();
    public string FolderName = string.Empty;
    private int _proj_code, _task_code;
    protected int _dept_code = 0;
    protected int _folder_code = 0;
    protected int _assgin_code = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 处理参数
        if (Request.QueryString["proj"] != null)
        {
            _proj_code = int.Parse(Request.QueryString["proj"]);
        }
        if (Request.QueryString["task"] != null)
        {
            _task_code = int.Parse(Request.QueryString["task"]);
            var firstOrDefault = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == _task_code);
            if (firstOrDefault != null)
            {
                _assgin_code = firstOrDefault.Assigned_To;
            }
        }
        if (Request.QueryString["dept"] != null)
        {
            _dept_code = int.Parse(Request.QueryString["dept"]);
        }
        if (Request.QueryString["folder"] != null)
        {
            _folder_code = int.Parse(Request.QueryString["folder"]);
            var firstOrDefault = _myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == _folder_code);
            if (firstOrDefault != null)
            {
                FolderName = firstOrDefault.Folder_Title;
            }
        }

        #endregion

        var orDefault = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
        var _isAnony = orDefault != null && orDefault.Is_Anonymous;
        if (Author.department_Code == _dept_code || _isAnony)
        {
            #region 绑定数据

            var documentDatasource =
                from d in _myDmsEntity.Document.Where(d => d.Folder_Code == _folder_code)
                select new
                           {
                               d.Document_Code,
                               d.Document_Name,
                               d.Document_Desc,
                               OwnerName =
                    _myDmsEntity.User.FirstOrDefault(uu => uu.User_Code == d.Owner).FirstName +
                    _myDmsEntity.User.FirstOrDefault(uu => uu.User_Code == d.Owner).LastName,
                               DocumentType =
                    _myDmsEntity.DocumentType.FirstOrDefault(dt => dt.DT_Code == d.Document_Type).Type_Name,
                               d.Creation_Date,
                               d.Updation_Date,
                               d.Recieved_Date
                           };
            gvDocuments.DataSource = documentDatasource;
            gvDocuments.DataBind();

            #endregion
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("./UploadDocument.aspx?proj={0}&task={1}&folder={2}", _proj_code, _task_code,_folder_code));
    }


    protected void btnShareDocument_Click(object sender, EventArgs e)
    {
        UserDocument uFolder = new UserDocument();
        uFolder.Shared_Date = DateTime.Now;
        uFolder.Shared_By = Author.user_Code;
        uFolder.Share_Comment = txtUserDocumentDesc.Text.Trim();
        uFolder.Is_Active = true;
        uFolder.User_Code = int.Parse(ddlUsers.SelectedItem.Value);


        _myDmsEntity.UserDocument.AddObject(uFolder);
        if (_myDmsEntity.SaveChanges() > 0)
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');alert('公开成功');location.reload(true);");
        }
        else
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');javascript:alert('公开失败');location.reload(true);");
        }
    }
    protected void gvDocuments_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            //int documentCode = Convert.ToInt32(e.CommandArgument);

            //Document upDoc = _myDmsEntity.Document.FirstOrDefault(ud => ud.Document_Code == documentCode);
            Response.Redirect(string.Format("javascript:window.open( 'http://localhost:28062/MyDMS/viewdocument.aspx?doc={0} ', 'newwin ', 'toolbar=no,menubar=no,status=yes,location=no,resizable=no,scrollbars=yes,width=500,height=380,center:   Yes ');", Convert.ToString(e.CommandArgument)));
        }
        if (e.CommandName == "Del")
        {
            int documentCode = Convert.ToInt32(e.CommandArgument);

            Document upDoc = _myDmsEntity.Document.FirstOrDefault(ud => ud.Document_Code == documentCode);
            FileInfo file = new FileInfo(upDoc.File_Path);
            file.Delete();
            _myDmsEntity.Document.DeleteObject(upDoc);
            _myDmsEntity.SaveChanges();

            Task task = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == _task_code);
            task.Updation_Date = DateTime.Now;
            _myDmsEntity.SaveChanges();

            Project project = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
            project.Updation_Date = DateTime.Now;
            _myDmsEntity.SaveChanges();

            Folder folder = _myDmsEntity.Folder.FirstOrDefault(f => f.Folder_Code == _folder_code);
            folder.Updation_Date = DateTime.Now;
            _myDmsEntity.SaveChanges();

            gvDocuments.DataBind(); 
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
    protected void gvDocuments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvDocuments.PageIndex = e.NewPageIndex;
    }
}